<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

	<title>Module: Rask</title>

	<link rel="stylesheet" href="./rdoc.css" type="text/css" media="screen" />

	<script src="./js/jquery.js" type="text/javascript"
		charset="utf-8"></script>
	<script src="./js/thickbox-compressed.js" type="text/javascript"
		charset="utf-8"></script>
	<script src="./js/quicksearch.js" type="text/javascript"
		charset="utf-8"></script>
	<script src="./js/darkfish.js" type="text/javascript"
		charset="utf-8"></script>

</head>
<body class="module">

	<div id="metadata">
		<div id="file-metadata">
			<div id="file-list-section" class="section">
				<h3 class="section-header">In Files</h3>
				<div class="section-body">
					<ul>
					
						<li><a href="./lib/rask_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
							class="thickbox" title="lib/rask.rb">lib/rask.rb</a></li>
					
					</ul>
				</div>
			</div>

			
		</div>

		<div id="class-metadata">

			<!-- Parent Class -->
			

			<!-- Namespace Contents -->
			
			<div id="namespace-list-section" class="section">
				<h3 class="section-header">Namespace</h3>
				<ul class="link-list">
					
					<li><span class="type">CLASS</span> <a href="Rask/Task.html">Rask::Task</a></li>
					
				</ul>
			</div>
			

			<!-- Method Quickref -->
			
			<div id="method-list-section" class="section">
				<h3 class="section-header">Methods</h3>
				<ul class="link-list">
					
					<li><a href="#M000008">::base_directory</a></li>
					
					<li><a href="#M000007">::base_directory=</a></li>
					
					<li><a href="#M000018">::daemon</a></li>
					
					<li><a href="#M000017">::destroy</a></li>
					
					<li><a href="#M000019">::initialize_storage</a></li>
					
					<li><a href="#M000012">::insert</a></li>
					
					<li><a href="#M000011">::pid_path</a></li>
					
					<li><a href="#M000015">::read</a></li>
					
					<li><a href="#M000013">::run</a></li>
					
					<li><a href="#M000014">::run_all</a></li>
					
					<li><a href="#M000020">::safe_class_name</a></li>
					
					<li><a href="#M000022">::safe_exit</a></li>
					
					<li><a href="#M000016">::task_ids</a></li>
					
					<li><a href="#M000010">::task_path</a></li>
					
					<li><a href="#M000009">::thread_max_count=</a></li>
					
					<li><a href="#M000021">::thread_proc</a></li>
					
				</ul>
			</div>
			

			<!-- Included Modules -->
			
		</div>

		<div id="project-metadata">
			
			

			<div id="classindex-section" class="section project-section">
				<h3 class="section-header">Class Index
					<span class="search-toggle"><img src="./images/find.png"
						height="16" width="16" alt="[+]"
						title="show/hide quicksearch" /></span></h3>
				<form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
				<fieldset>
					<legend>Quicksearch</legend>
					<input type="text" name="quicksearch" value=""
						class="quicksearch-field" />
				</fieldset>
				</form>

				<ul class="link-list">
				
					<li><a href="./Rask.html">Rask</a></li>
				
					<li><a href="./Rask/Task.html">Rask::Task</a></li>
				
				</ul>
				<div id="no-class-search-results" style="display: none;">No matching classes.</div>
			</div>

			
		</div>
	</div>

	<div id="documentation">
		<h1 class="module">Rask</h1>

		<div id="description">
			<table>
<tr><td valign="top">Authors:</td><td>mewlist / Hidenori Doi

</td></tr>
<tr><td valign="top">Copyright:</td><td>Copyright (C) 2010 mewlist / Hidenori Doi

</td></tr>
<tr><td valign="top">License:</td><td>The MIT License

</td></tr>
</table>
<h2><a href="Rask.html">Rask</a> is terminatable task engine</h2>
<h4>sample code</h4>
<pre>
 require 'rubygems'
 require 'rask'

 # task of count up to 10
 class CountupTask &lt; Rask::Task
   # define statemachine states
   define_state :start,   :initial =&gt; true       # initial state
   define_state :running                         # run
   define_state :finish,  :from    =&gt; [:running] # finish (only from :running)

   def start # same name as state definition(define_state)
     @count = 0
     p &quot;start&quot;
     transition_to_running # transition to :running
   end

   def running
     p &quot;running count =&gt; #{@count+=1}&quot;
     transition_to_finish if @count&gt;=10 # transition to :finish
   end

   def finish
     p &quot;finished&quot;
     destroy # destroy task myself
   end
 end

 Rask.insert CountupTask.new # insert the task

 Rask.daemon # run as a daemon
</pre>

		</div>

		<!-- Constants -->
		

		<!-- Attributes -->
		

		<!-- Methods -->
		
		<div id="public-class-method-details" class="method-section section">
			<h3 class="section-header">Public Class Methods</h3>

		
			<div id="base-directory-method" class="method-detail ">
				<a name="M000008"></a>

				<div class="method-heading">
				
					<span class="method-name">base_directory</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<p class="missing-docs">(Not documented)</p>
					

					
					<div class="method-source-code"
						id="base-directory-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 148</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">base_directory</span>
    <span class="ruby-ivar">@@base_dir</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="base-directory--method" class="method-detail ">
				<a name="M000007"></a>

				<div class="method-heading">
				
					<span class="method-name">base_directory=</span><span
						class="method-args">(new_directory)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<h3>Set/Get base storage directory</h3>
<table>
<tr><td valign="top">default :</td><td>/tmp/rask

</td></tr>
</table>
					

					
					<div class="method-source-code"
						id="base-directory--source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 144</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">base_directory=</span>(<span class="ruby-identifier">new_directory</span>)
    <span class="ruby-ivar">@@base_dir</span> = <span class="ruby-identifier">new_directory</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="daemon-method" class="method-detail ">
				<a name="M000018"></a>

				<div class="method-heading">
				
					<span class="method-name">daemon</span><span
						class="method-args">(options = {:class=>nil, :group=>nil, :sleep=>0.1, :worker_sleep=>0.1, :process_name=>nil})</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<h3>Start a daemon process</h3>
<dl>
<dt>options</dt><dd>class :: Only the instance of specified class.

<table>
<tr><td valign="top">group :</td><td>Only the instance of specified group. see also Task::initialize.

</td></tr>
<tr><td valign="top">sleep :</td><td>Polling interval daemon process.

</td></tr>
</table>
</dd>
</dl>
					

					
					<div class="method-source-code"
						id="daemon-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 273</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">daemon</span>(<span class="ruby-identifier">options</span> = {<span class="ruby-identifier">:class</span>=<span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:group=</span><span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:sleep=</span><span class="ruby-operator">&gt;</span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:worker_sleep=</span><span class="ruby-operator">&gt;</span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:process_name=</span><span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span>})
    <span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:sleep=</span><span class="ruby-operator">&gt;</span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:worker_sleep=</span><span class="ruby-operator">&gt;</span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:process_name=</span><span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:threading=</span><span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">true</span> }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
    <span class="ruby-identifier">print</span> <span class="ruby-value str">&quot;daemon start\n&quot;</span>
    <span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">fork</span>
    <span class="ruby-constant">Process</span>.<span class="ruby-identifier">setsid</span>
    
    <span class="ruby-identifier">initialize_storage</span>
    <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span> <span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:process_name</span>])
      <span class="ruby-identifier">print</span> <span class="ruby-node">&quot;already running rask process. #{File.basename($0)}&quot;</span>
      <span class="ruby-keyword kw">return</span>
    <span class="ruby-keyword kw">end</span>
    <span class="ruby-identifier">open</span>(<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:process_name</span>]),<span class="ruby-value str">&quot;w&quot;</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">write</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">pid</span>}
    
    <span class="ruby-comment cmt"># create worker threads</span>
    <span class="ruby-identifier">threads</span> = []
    <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword kw">in</span> <span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-ivar">@@thread_max_count</span> <span class="ruby-keyword kw">do</span> 
      <span class="ruby-identifier">threads</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Thread</span><span class="ruby-operator">::</span><span class="ruby-identifier">new</span>(<span class="ruby-identifier">i</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">thread_id</span><span class="ruby-operator">|</span> <span class="ruby-identifier">thread_proc</span>(<span class="ruby-identifier">thread_id</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:worker_sleep</span>]) }
    <span class="ruby-keyword kw">end</span>
    
    <span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>(<span class="ruby-identifier">:TERM</span>) {<span class="ruby-identifier">safe_exit</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:process_name</span>])}
    
    <span class="ruby-keyword kw">while</span> <span class="ruby-keyword kw">true</span>
      <span class="ruby-identifier">task_list</span> = <span class="ruby-constant">Rask</span>.<span class="ruby-identifier">task_ids</span>(<span class="ruby-identifier">options</span>)
      <span class="ruby-identifier">task_list</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">d</span><span class="ruby-operator">|</span>
        <span class="ruby-ivar">@@locker</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
          <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@@processing</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">d</span>)
            <span class="ruby-ivar">@@queue</span>.<span class="ruby-identifier">push</span> <span class="ruby-identifier">d</span>
            <span class="ruby-ivar">@@processing</span>.<span class="ruby-identifier">push</span> <span class="ruby-identifier">d</span>
          <span class="ruby-keyword kw">end</span>
        <span class="ruby-keyword kw">end</span>
      }
      <span class="ruby-identifier">sleep</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:sleep</span>])
    <span class="ruby-keyword kw">end</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="destroy-method" class="method-detail ">
				<a name="M000017"></a>

				<div class="method-heading">
				
					<span class="method-name">destroy</span><span
						class="method-args">(task)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<h3>force destroy the task</h3>
					

					
					<div class="method-source-code"
						id="destroy-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 262</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">destroy</span>(<span class="ruby-identifier">task</span>)
    <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task</span>.<span class="ruby-identifier">task_id</span>)) <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task</span>.<span class="ruby-identifier">task_id</span>)
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="insert-method" class="method-detail ">
				<a name="M000012"></a>

				<div class="method-heading">
				
					<span class="method-name">insert</span><span
						class="method-args">(task)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<h3>Insert a new task. The task will be controlled under <a href="Rask.html">Rask</a> daemon process.</h3>
<h4>sample code</h4>
<pre>
 Rask::insert NewTask.new
</pre>
					

					
					<div class="method-source-code"
						id="insert-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 176</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">insert</span>(<span class="ruby-identifier">task</span>)
    <span class="ruby-identifier">initialize_storage</span>
    <span class="ruby-identifier">task_id</span> = <span class="ruby-node">&quot;#{safe_class_name(task.class.name)}-#{task.group.to_s}-#{Time.now.to_i}-#{Time.now.usec}&quot;</span>
    <span class="ruby-identifier">task</span>.<span class="ruby-identifier">task_id</span> = <span class="ruby-identifier">task_id</span>
    <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">touch</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>)) <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>)
    <span class="ruby-identifier">f</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>), <span class="ruby-value str">'w'</span>)
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_EX</span>)
    <span class="ruby-constant">Marshal</span>.<span class="ruby-identifier">dump</span>(<span class="ruby-identifier">task</span>, <span class="ruby-identifier">f</span>)
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_UN</span>)
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="pid-path-method" class="method-detail ">
				<a name="M000011"></a>

				<div class="method-heading">
				
					<span class="method-name">pid_path</span><span
						class="method-args">(process_name = nil)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="pid-path-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 166</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">process_name</span> = <span class="ruby-keyword kw">nil</span>)
    <span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-node">&quot;/#{File.basename($0)}.pid&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">process_name</span><span class="ruby-operator">==</span><span class="ruby-keyword kw">nil</span>
    <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-node">&quot;/#{process_name}.pid&quot;</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="read-method" class="method-detail ">
				<a name="M000015"></a>

				<div class="method-heading">
				
					<span class="method-name">read</span><span
						class="method-args">(task_id)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<h3>Get the task instance to observe.</h3>
<p>
You can use the instance for only the purpose of observation.
</p>
					

					
					<div class="method-source-code"
						id="read-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 224</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">task_id</span>)
    <span class="ruby-identifier">f</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>), <span class="ruby-value str">'r+'</span>) <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">return</span>
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_EX</span>)
    <span class="ruby-identifier">task</span> = <span class="ruby-constant">Marshal</span>.<span class="ruby-identifier">restore</span>(<span class="ruby-identifier">f</span>)
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_UN</span>)
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">task</span>.<span class="ruby-identifier">read_only</span> = <span class="ruby-keyword kw">true</span>
    <span class="ruby-identifier">task</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="run-method" class="method-detail ">
				<a name="M000013"></a>

				<div class="method-heading">
				
					<span class="method-name">run</span><span
						class="method-args">(task_id)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="run-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 190</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">run</span>(<span class="ruby-identifier">task_id</span>)
    <span class="ruby-identifier">f</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>), <span class="ruby-value str">'r+'</span>) <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">return</span>
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_EX</span>)
    <span class="ruby-identifier">task</span> = <span class="ruby-constant">Marshal</span>.<span class="ruby-identifier">restore</span>(<span class="ruby-identifier">f</span>)
    <span class="ruby-keyword kw">begin</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
        <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">task</span>
      <span class="ruby-keyword kw">else</span>
        <span class="ruby-identifier">task</span>.<span class="ruby-identifier">run</span>
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-identifier">f</span>.<span class="ruby-identifier">truncate</span>(<span class="ruby-value">0</span>)
      <span class="ruby-identifier">f</span>.<span class="ruby-identifier">pos</span> = <span class="ruby-value">0</span>
      <span class="ruby-constant">Marshal</span>.<span class="ruby-identifier">dump</span>(<span class="ruby-identifier">task</span>, <span class="ruby-identifier">f</span>)
      <span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_UN</span>)
      <span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
      <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>)) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">task</span>.<span class="ruby-identifier">destroy?</span>
    <span class="ruby-keyword kw">rescue</span>
      <span class="ruby-identifier">p</span> <span class="ruby-identifier">$!</span>
      <span class="ruby-identifier">print</span> <span class="ruby-identifier">$@</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;\n&quot;</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;\n--------------------------------------------\n&quot;</span>
      <span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_UN</span>)
      <span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
      <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">mv</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>), <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;/suspended/&quot;</span>)
    <span class="ruby-keyword kw">end</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="run-all-method" class="method-detail ">
				<a name="M000014"></a>

				<div class="method-heading">
				
					<span class="method-name">run_all</span><span
						class="method-args">(options = { :class=>nil, :group=>nil })</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="run-all-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 216</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">run_all</span>(<span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:class</span>=<span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:group=</span><span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span> })
    <span class="ruby-constant">Rask</span>.<span class="ruby-identifier">task_ids</span>(<span class="ruby-identifier">options</span>).<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">task_id</span><span class="ruby-operator">|</span> <span class="ruby-identifier">run</span>(<span class="ruby-identifier">task_id</span>) }
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="task-ids-method" class="method-detail ">
				<a name="M000016"></a>

				<div class="method-heading">
				
					<span class="method-name">task_ids</span><span
						class="method-args">(options = { :class=>nil, :group=>nil })</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<h3>Get task_id list.</h3>
<dl>
<dt>options</dt><dd>class :: Only the instance of specified class.

<table>
<tr><td valign="top">group :</td><td>Only the instance of specified group. see also Task::initialize

</td></tr>
</table>
</dd>
</dl>
					

					
					<div class="method-source-code"
						id="task-ids-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 240</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">task_ids</span>(<span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:class</span>=<span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:group=</span><span class="ruby-operator">&gt;</span><span class="ruby-keyword kw">nil</span> })
    <span class="ruby-identifier">target</span> = <span class="ruby-ivar">@@base_dir</span>
    <span class="ruby-identifier">target</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">'/'</span>
    <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class</span>]
      <span class="ruby-identifier">target</span> <span class="ruby-operator">+=</span> <span class="ruby-node">&quot;#{safe_class_name(options[:class])}&quot;</span>
    <span class="ruby-keyword kw">else</span>
      <span class="ruby-identifier">target</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;[^-]+&quot;</span>
    <span class="ruby-keyword kw">end</span>
    <span class="ruby-identifier">target</span> <span class="ruby-operator">+=</span> <span class="ruby-node">&quot;-#{options[:group]}-&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:group</span>]
    
    <span class="ruby-identifier">task_id_list</span> = []
    <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">glob</span>(<span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;/*.task&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">d</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">target</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> <span class="ruby-node">/#{target}/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">d</span>
        <span class="ruby-identifier">task_id_list</span>.<span class="ruby-identifier">push</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">basename</span>(<span class="ruby-identifier">d</span>, <span class="ruby-value str">&quot;.*&quot;</span>)
      <span class="ruby-keyword kw">end</span>
    }
    <span class="ruby-identifier">task_id_list</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="task-path-method" class="method-detail ">
				<a name="M000010"></a>

				<div class="method-heading">
				
					<span class="method-name">task_path</span><span
						class="method-args">(task_id)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="task-path-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 161</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>)
    <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-node">&quot;/#{task_id}.task&quot;</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="thread-max-count--method" class="method-detail ">
				<a name="M000009"></a>

				<div class="method-heading">
				
					<span class="method-name">thread_max_count=</span><span
						class="method-args">(count)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					<h3>Set max count of worker thread</h3>
<table>
<tr><td valign="top">default :</td><td>5

</td></tr>
</table>
					

					
					<div class="method-source-code"
						id="thread-max-count--source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 156</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">thread_max_count=</span>(<span class="ruby-identifier">count</span>)
    <span class="ruby-ivar">@@thread_max_count</span> = <span class="ruby-identifier">count</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
		</div>
	
		<div id="private-class-method-details" class="method-section section">
			<h3 class="section-header">Private Class Methods</h3>

		
			<div id="initialize-storage-method" class="method-detail ">
				<a name="M000019"></a>

				<div class="method-heading">
				
					<span class="method-name">initialize_storage</span><span
						class="method-args">()</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="initialize-storage-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 310</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">initialize_storage</span>
    <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">makedirs</span> <span class="ruby-ivar">@@base_dir</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-ivar">@@base_dir</span>
    <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">makedirs</span> <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;/suspended&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;/suspended&quot;</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="safe-class-name-method" class="method-detail ">
				<a name="M000020"></a>

				<div class="method-heading">
				
					<span class="method-name">safe_class_name</span><span
						class="method-args">(c)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="safe-class-name-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 316</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">safe_class_name</span>(<span class="ruby-identifier">c</span>)
    <span class="ruby-identifier">c</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/[:]/</span>,<span class="ruby-value str">'@'</span>)
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="safe-exit-method" class="method-detail ">
				<a name="M000022"></a>

				<div class="method-heading">
				
					<span class="method-name">safe_exit</span><span
						class="method-args">(process_name)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="safe-exit-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 344</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">safe_exit</span>(<span class="ruby-identifier">process_name</span>)
    <span class="ruby-ivar">@@terminated</span> = <span class="ruby-keyword kw">true</span>
    <span class="ruby-keyword kw">while</span> <span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
      <span class="ruby-identifier">sleep</span>(<span class="ruby-value">0</span><span class="ruby-value">.1</span>)
    <span class="ruby-keyword kw">end</span>
    <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">process_name</span>)) <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">process_name</span>))
    <span class="ruby-identifier">print</span> <span class="ruby-value str">&quot;[Rask]  safely daemon terminated. \n&quot;</span>
    <span class="ruby-identifier">exit</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
			<div id="thread-proc-method" class="method-detail ">
				<a name="M000021"></a>

				<div class="method-heading">
				
					<span class="method-name">thread_proc</span><span
						class="method-args">(thread_id, worker_sleep = 0.1)</span>
					<span class="method-click-advice">click to toggle source</span>
				
				</div>

				<div class="method-description">
					
					
					

					
					<div class="method-source-code"
						id="thread-proc-source">
<pre>
<span class="ruby-comment cmt"># File lib/rask.rb, line 321</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">thread_proc</span>(<span class="ruby-identifier">thread_id</span>, <span class="ruby-identifier">worker_sleep</span> = <span class="ruby-value">0</span><span class="ruby-value">.1</span>)
    <span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
    <span class="ruby-identifier">print</span> <span class="ruby-node">&quot;[Rask] Thread Start ID:#{thread_id}\n&quot;</span>
    <span class="ruby-keyword kw">while</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@@terminated</span>
      <span class="ruby-identifier">d</span> = <span class="ruby-keyword kw">nil</span>
      <span class="ruby-ivar">@@locker</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
        <span class="ruby-identifier">d</span> = <span class="ruby-ivar">@@queue</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@@queue</span>.<span class="ruby-identifier">empty?</span>
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">d</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">nil</span>
        <span class="ruby-comment cmt">#print &quot;task #{d}\n&quot;</span>
        <span class="ruby-identifier">run</span>(<span class="ruby-identifier">d</span>)
        <span class="ruby-ivar">@@locker</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
          <span class="ruby-ivar">@@processing</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">d</span>)
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">else</span>
      <span class="ruby-comment cmt"># print &quot;no data in queue\n&quot;</span>
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-identifier">sleep</span>(<span class="ruby-identifier">worker_sleep</span>)
    <span class="ruby-keyword kw">end</span>
    <span class="ruby-identifier">print</span> <span class="ruby-node">&quot;[Rask] Thread Exit ID:#{thread_id}\n&quot;</span>
    <span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
  <span class="ruby-keyword kw">end</span></pre>
					</div>
					
				</div>

				
			</div>

		
		</div>
	

	</div>


	<div id="rdoc-debugging-section-dump" class="debugging-section">
	
		<p>Disabled; run with --debug to generate this.</p>
	
	</div>

	<div id="validator-badges">
		<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
		<p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
			Rdoc Generator</a> 1.1.6</small>.</p>
	</div>

</body>
</html>

